package androidlab.sprintingsquirrel.game;

import java.text.DecimalFormat;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.HorizontalAlign;

import androidlab.sprintingsquirrel.BaseScene;
import androidlab.sprintingsquirrel.ResourceManager;
import androidlab.sprintingsquirrel.SceneManager;

public class LevelCompleteWindow extends Sprite {
	
	private Sprite restartLevelButton;
	private TiledSprite hazelnut1;
	private TiledSprite hazelnut2;
	private TiledSprite hazelnut3;
	private Text timerText;
	private Text hazelAmount;
	private BaseScene scene;
	    
	    public enum HazelnutCount {
	        ONE,
	        TWO,
	        THREE
	    }
	    
	    public LevelCompleteWindow(VertexBufferObjectManager pSpriteVertexBufferObject, BaseScene scene) {
	        super(0, 0, 705, 405, ResourceManager.getInstance().completedWindowTR, pSpriteVertexBufferObject);
	        this.scene = scene;
	        attachHazelnuts(pSpriteVertexBufferObject);
	        attachText(pSpriteVertexBufferObject);
	    }
	    
	    private void attachHazelnuts(VertexBufferObjectManager pSpriteVertexBufferObject) {
	    	hazelnut1 = new TiledSprite(252, 150, ResourceManager.getInstance().completedHazelnutsTTR, pSpriteVertexBufferObject);
	    	hazelnut2 = new TiledSprite(332, 150, ResourceManager.getInstance().completedHazelnutsTTR, pSpriteVertexBufferObject);
	    	hazelnut3 = new TiledSprite(412, 150, ResourceManager.getInstance().completedHazelnutsTTR, pSpriteVertexBufferObject);
	    	restartLevelButton = new Sprite(0, 0, ResourceManager.getInstance().restartButtonTR, pSpriteVertexBufferObject){
				@Override
				public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					SceneManager.getInstance().createGameScene(ResourceManager.getInstance().getLevel());
					scene.disposeScene();
					ResourceManager.getInstance().loadLoadingTextures();
					return false;
				}
			};
	        
	    	hazelnut1.setScale(1.3f);
	    	hazelnut2.setScale(1.3f);
	    	hazelnut3.setScale(1.3f);
	    	restartLevelButton.setScale(0.3f);
	    	
	        attachChild(hazelnut1);
	        attachChild(hazelnut2);
	        attachChild(hazelnut3);
	        attachChild(restartLevelButton);
	        scene.registerTouchArea(restartLevelButton);
	    }
	    
	    private void attachText(VertexBufferObjectManager pSpriteVertexBufferObject) {
	    	
	    	timerText = new Text(-245, 160, ResourceManager.getInstance().font_cw, "Deine Zeit 00:00", pSpriteVertexBufferObject);
	        timerText.setSkew(0, 0);
			timerText.setScale(0.2f);
			
			hazelAmount = new Text(-905, 200 , ResourceManager.getInstance().font_cw, "Gesammelte Nuesse 000 von 000", new TextOptions(HorizontalAlign.RIGHT), pSpriteVertexBufferObject);
			hazelAmount.setSkew(0, 0);
			hazelAmount.setScale(0.2f);
			
			attachChild(timerText);
			attachChild(hazelAmount);
			
	    }
	    
	    public void display(HazelnutCount hazelnutsCount, Scene scene, Camera camera, int time, int hazelCounter, int hazelsTotal) {
	    	
	    	final DecimalFormat df = new DecimalFormat("00");
	    	final DecimalFormat df2 = new DecimalFormat("000");
	    	
	        switch (hazelnutsCount) {
	            case ONE:
	                hazelnut1.setCurrentTileIndex(0);
	                hazelnut2.setCurrentTileIndex(1);
	                hazelnut3.setCurrentTileIndex(1);
	                break;
	            case TWO:
	            	hazelnut1.setCurrentTileIndex(0);
	            	hazelnut2.setCurrentTileIndex(0);
	            	hazelnut3.setCurrentTileIndex(1);
	                break;
	            case THREE:
	            	hazelnut1.setCurrentTileIndex(0);
	            	hazelnut2.setCurrentTileIndex(0);
	            	hazelnut3.setCurrentTileIndex(0);
	                break;
	        }
	        setPosition(camera.getCenterX()-ResourceManager.getInstance().completedWindowTR.getWidth()/2, camera.getCenterY()-ResourceManager.getInstance().completedWindowTR.getHeight()/2);
			timerText.setText("Deine Zeit " + df.format(time/60) + ":" + df.format(time%60));
			hazelAmount.setText("Gesammelte Nuesse " + df2.format(hazelCounter) + " von " + df2.format(hazelsTotal));
			restartLevelButton.setPosition(515, 215);
			scene.attachChild(this);

	    }
}
